package com.gxa.controller.commlife;

import com.gxa.common.utils.Result;
import com.gxa.dto.commlife.BusinessManagementDto;
import com.gxa.entity.commlife.BusinessManagement;
import com.gxa.service.commlife.BusinessManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Api(tags = "商家管理")
@RestController
@RequestMapping("/business-management")
public class BusinessManagementController {

    @Autowired
    private BusinessManagementService businessManagementService;

//    List<BusinessManagement> businessManagementList;
//    Date date = new Date("2022/11/12");
//    public BusinessManagementController(){
//        this.init();
//    }
//    void init(){
//        businessManagementList = new ArrayList<>();
//        MerchantType merchantType = new MerchantType(1,"餐饮服务",date,0);
//        BusinessManagement businessManagement = new BusinessManagement(1,"张三丰美食店",merchantType,"广州市天河区天河路666号","116°E,40°N","08:00 ~ 18:00","粤式茶点",0,0);
//        businessManagementList.add(businessManagement);
//        MerchantType merchantType1 = new MerchantType(2,"餐饮服务",date,0);
//        BusinessManagement businessManagement1 = new BusinessManagement(2,"张三丰美食店",merchantType,"广州市天河区天河路666号","116°E,40°N","08:00 ~ 18:00","粤式茶点",0,0);
//        businessManagementList.add(businessManagement1);
//    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询商家管理")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
     try {
         Map<String, Object> map = this.businessManagementService.queryList(pageNum, pageSize);
         Result<Map<String, Object>> result = Result.success(map);
         return result;
     }catch (Exception e){
         e.printStackTrace();
         return Result.failed("查询失败");
     }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @PostMapping("search")
    @ApiOperation(value = "搜索商家管理")
    public Result search(@RequestBody BusinessManagement businessManagement,Integer pageNum,Integer pageSize){
        try {
            Map<String, Object> map = this.businessManagementService.queryListBySearch(businessManagement, pageNum, pageSize);
            Result<Map<String, Object>> result = Result.success(map);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("搜索失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "增加商家管理")
    @PostMapping("add")
    public Result<Object> add(@RequestBody BusinessManagement businessManagement){
        try {
            this.businessManagementService.addBusinessManagement(businessManagement);
            Result<Object> result = Result.success("添加成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询当前商家管理")
    @GetMapping("preEdit/{id}")
    public Result preEdit(@PathVariable("id") Integer id){
        try {
            BusinessManagementDto businessManagementDto = this.businessManagementService.queryById(id);
            Result<BusinessManagementDto> result = Result.success(businessManagementDto);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("当前用户查询失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "修改商家管理")
    @PutMapping("edit")
    public Result edit(@RequestBody BusinessManagement businessManagement){
        try {
            this.businessManagementService.updateBusinessManagement(businessManagement);
            Result<Object> result = Result.success("修改成功");
            result.setMsg("修改成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("修改失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "删除商家管理")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        try {
            this.businessManagementService.deleteBusinessManagement(id);
            Result<Object> result = Result.success("删除成功");
            result.setMsg("删除成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("删除失败");
        }
    }

}
